package com.bookmall.search.controller;

import com.bookmall.common.constant.ExceptionTypeConstants;
import com.bookmall.common.entity.es.EsModel;
import com.bookmall.search.service.IElasticSearchService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-08-15:49
 */
@RestController
@RequestMapping("/elastic/book")
public class ElasticSearchController extends BaseController {
    @Autowired
    private IElasticSearchService elasticSearchSaveService;

    /**
     * 上架书籍
     * @author 胡菜鸡
     * @date 2022/4/8 15:50
     */
    @PostMapping("/save")
    public AjaxResult bookStatusUp(@RequestBody List<EsModel> esModels){
        boolean result;
        try {
            result = elasticSearchSaveService.bookStatusUp(esModels);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(ExceptionTypeConstants.BOOK_UP_FAIL, "书籍上架失败");
        }

        if(result) return AjaxResult.success();
        else return AjaxResult.error(ExceptionTypeConstants.BOOK_UP_FAIL, "书籍上架失败");
    }
}
